<template>
  <div class="list-table">
    <div class=" area-header">
      <span class="area-header-title">{{ $t('流程列表') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('流程名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          :placeholder="$t('流程名称')"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('流程标识')" prop="key">
        <el-input
          v-model="queryParams.key"
          clearable
          :placeholder="$t('流程标识')"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>

      <el-form-item :label="$t('流程分类')" prop="category">
        <dictSelect
          v-model="queryParams.category"
          :clearable="true"
          dictionary="act_model_type"
          :placeholder="$t('请选择')"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button icon="el-icon-delete" type="danger" size="mini" :disabled="multiple" @click="handleBacthDelete">{{ $t("批量删除") }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="actProcessList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column :label="$t('流程名称')" align="center" prop="name" />
      <el-table-column :label="$t('流程标识')" align="center" prop="key" />
      <el-table-column :label="$t('流程分类')" align="center" prop="category" :formatter="categoryFormat" />
      <el-table-column :label="$t('版本号')" align="center" prop="version" />
      <el-table-column :label="$t('部署时间')" align="center" prop="deployTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deployTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-document"
            @click="xmlHandle(scope.row.id)"
          >{{ $t('查看XML') }}</el-button>

          <el-button
            v-hasPermi="['act:model:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="$t('流程XML')" :close-on-click-modal="false" :visible.sync="xmlOpen" width="70%" custom-class="dialogForm">
      <div style="overflow: auto">
        <xmp><div v-text="processXmlData"></div></xmp>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 引入和后台交互的JS
  import { listProcess, getXml, delProcess, batchDelProcess } from '@/api/act/process/actProcess'
  export default {
    components: {

    },
    data() {
      return {
        // 流程Xml页面是否展示
        xmlOpen: false,
        // 流程Xml数据
        processXmlData: '',
        // 遮罩层
        loading: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 流程数据
        actProcessList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 模型类型字典
        categoryOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          place: undefined,
          category: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        // 批量删除的Id数组
        deleteIds: []
      }
    },
    created() {
      this.getList()
      this.getDicts('act_model_type').then(response => {
        this.categoryOptions = response.data
      })
    },
    methods: {
      /** 查询模型列表 */
      getList() {
        this.loading = true
        listProcess(this.queryParams).then(response => {
          this.actProcessList = response.rows
          this.total = response.total
          this.loading = false
        })
      },

      categoryFormat(row) {
        return this.selectDictLabel(this.categoryOptions, row.category)
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          place: undefined,
          phone: undefined,
          largestNumber: undefined,
          callMeeting: '0',
          videoMeeting: '0',
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
      },
      // 查看xml
      xmlHandle(procDefId) {
        this.xmlOpen = true
        getXml(procDefId).then(response => {
          this.processXmlData = response.data
        })
      },
      /** 删除按钮操作 */
      deleteHandle(row) {
        console.log(row)
        // const ids = row.id || this.ids
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return delProcess(row.deploymentId)
        }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {})
      },
      /** 删除按钮操作 */
      handleBacthDelete() {
        const deleteIds = this.deleteIds
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return batchDelProcess(deleteIds)
        }).then((response) => {
          this.getList()
          // this.msgSuccess(this.$t('删除成功'))
          this.$notify({
              title: '',
              dangerouslyUseHTMLString: true,
              message: this.$t(response.msg),
              duration: 4500
            })
        }).catch(function() {})
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.deleteIds = selection.map(item => item.deploymentId)
        this.multiple = !selection.length
      }

    }
  }
</script>
